package com.pxmeta.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pxmeta.app.entity.AppVersion;
import com.pxmeta.app.entity.dto.AddVersionDTO;
import com.pxmeta.app.entity.dto.UpdateVersionDTO;
import com.pxmeta.app.service.IAppVersionService;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.StringUtils;
import com.pxmeta.common.utils.bean.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * App 版本控制器
 */
@RestController
@Api(value = "App版本",tags = "App版本")
@RequiredArgsConstructor
@RequestMapping("/version")
public class AppVersionController {

    private final IAppVersionService appVersionService;


    @GetMapping("/getVersionList")
    @ApiOperation("获取版本列表")
    public AjaxResult getVersionList(@RequestParam String platform){
        List<AppVersion> list = appVersionService.getListByPlatform(platform);
        return AjaxResult.success(list);
    }

    @GetMapping("/addVersion")
    @ApiOperation("添加版本")
    public AjaxResult addVersion(@RequestBody AddVersionDTO dto){
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(dto, appVersion);
        appVersionService.save(appVersion);
        return AjaxResult.success();
    }

    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("删除版本")
    public AjaxResult deleteById(@PathVariable Integer id){
        appVersionService.removeById(id);
        return AjaxResult.success();
    }

    @PutMapping("/updateVersion")
    @ApiOperation("修改版本")
    public AjaxResult updateVersion(@RequestBody @Validated UpdateVersionDTO dto){
        Integer id = dto.getId();
        Integer isNewest = dto.getIsNewest();
        String explain = dto.getExplain();
        appVersionService.updateVersion(id, explain, isNewest);
        return AjaxResult.success();
    }

}
